package GUI;

import GUITOOl.MyBirthdayChooseBox;
import GUITOOl.MyButton;
import GUITOOl.MyLineBorder;
import Service.Register;
import Utils.GetWindowsSize;
import Utils.ImageIconUtils;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.io.IOException;

/**
 * The type Register frame.
 *
 * @author dzr, wyc, zry
 * @version 1.0
 */
public class RegisterFrame extends JFrame{

    private JLabel name_lable;
    private JLabel pwd_lable;
    private JLabel pwd_twice_lable;
    private JLabel sex_lable;
    private JLabel birthday_lable;

    private JTextField name_text;
    private JPasswordField pwd_text;
    private JPasswordField pwd_twice_text;
    private JRadioButton boy_button;
    private JRadioButton girl_button;
    private ButtonGroup sex_button;
    private MyBirthdayChooseBox birthdayChooseBox;
    private JButton register_button;
    private JButton return_login;

    /**
     * The Box main.
     */
    Box boxMain, /**
     * The Box left.
     */
    boxLeft, /**
     * The Box right.
     */
    boxRight, /**
     * The Box sex.
     */
    boxSex, /**
     * The Box right birth.
     */
    boxRight_birth, /**
     * The Box up.
     */
    boxUp, /**
     * The Box down.
     */
    boxDown, /**
     * The Box bottom.
     */
    boxBottom;

    /**
     * Init.
     */
    public void init(){

        MyLineBorder textLineBorder = new MyLineBorder(new Color(192, 192, 192), 1 , true);
        Color bgColor=new Color(255,255,255);
        setTitle("feather_char_register");
        setSize(480,500);
        setLocation(GetWindowsSize.getWindowsSize().width / 2 - this.getWidth() / 2, GetWindowsSize.getWindowsSize().height / 2 - this.getHeight() / 2);
        getContentPane().setBackground(Color.white);
        setResizable(false);
        setLayout(new FlowLayout());
        Image frameImage=ImageIconUtils.getRawPicture("picture.png").getImage();
        setIconImage(frameImage);
        setVisible(true);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        name_lable = new JLabel("用户名");
        name_lable.setFont(new Font("微软雅黑",0,16));
        pwd_lable = new JLabel("密码");
        pwd_lable.setFont(new Font("微软雅黑",0,16));
        pwd_twice_lable = new JLabel("确认密码");
        pwd_twice_lable.setFont(new Font("微软雅黑",0,16));
        sex_lable = new JLabel("性别");
        sex_lable.setFont(new Font("微软雅黑",0,16));
        birthday_lable = new JLabel("出生日期");
        birthday_lable.setFont(new Font("微软雅黑",0,16));

        name_text = new JTextField(25);
        name_text.setBorder(textLineBorder);
        name_text.setBounds(50,15,150,30);
        pwd_text = new JPasswordField(25);
        pwd_text.setBorder(textLineBorder);
        pwd_text.setBounds(50, 50, 150, 30);
        pwd_twice_text = new JPasswordField(25);
        pwd_twice_text.setBorder(textLineBorder);
        pwd_twice_text.setBounds(50, 85, 150, 30);

        boy_button = new JRadioButton("男");
        boy_button.setFont(new Font("微软雅黑",0,16));
        boy_button.setSelected(true);
        boy_button.setBackground(bgColor);
        girl_button = new JRadioButton("女");
        girl_button.setFont(new Font("微软雅黑",0,16));
        girl_button.setBackground(bgColor);
        register_button = new MyButton("注册",1);
        register_button.setSize(50,30);
        register_button.addActionListener(new registerAction());
        return_login = new MyButton("返回",1);
        return_login.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                RegisterFrame.this.dispose();
            }
        });
        register_button.setSize(100,30);
        sex_button = new ButtonGroup();
        sex_button.add(boy_button);
        sex_button.add(girl_button);

        boxLeft = Box.createVerticalBox();
        boxLeft.add(Box.createVerticalStrut(20));
        boxLeft.add(name_lable);
        boxLeft.add(Box.createVerticalStrut(20));
        boxLeft.add(pwd_lable);
        boxLeft.add(Box.createVerticalStrut(25));
        boxLeft.add(pwd_twice_lable);
        boxLeft.add(Box.createVerticalStrut(25));
        boxLeft.add(birthday_lable);
        boxLeft.add(Box.createVerticalStrut(35));
        boxLeft.add(sex_lable);

        boxSex=Box.createHorizontalBox();
        boxRight = Box.createVerticalBox();
        boxRight.add(Box.createVerticalStrut(30));
        boxRight.add(name_text);
        boxRight.add(Box.createVerticalStrut(25));
        boxRight.add(pwd_text);
        boxRight.add(Box.createVerticalStrut(25));
        boxRight.add(pwd_twice_text);
        boxRight.add(Box.createVerticalStrut(25));

        birthdayChooseBox = new MyBirthdayChooseBox("2000", "01", "01");
        boxRight_birth = birthdayChooseBox.getBirthdayChooseBox();
        boxRight.add(boxRight_birth);
        boxSex.add(Box.createHorizontalStrut(3));
        boxSex.add(boy_button);
        boxSex.add(Box.createHorizontalStrut(8));
        boxSex.add(girl_button);
        boxRight.add(Box.createVerticalStrut(25));
        boxRight.add(boxSex);

        boxUp=Box.createHorizontalBox();
        boxUp.add(boxLeft);
        boxUp.add(Box.createHorizontalStrut(10));
        boxUp.add(boxRight);

        boxDown=Box.createHorizontalBox();
        boxDown.add(Box.createHorizontalStrut(3));
        boxDown.add(register_button);
        boxDown.add(Box.createHorizontalStrut(50));
        boxDown.add(return_login);

        JPanel panel_bottom=new JPanel();
        panel_bottom.setBackground(bgColor);
        ImageIcon image1= ImageIconUtils.getRawPicture("register_bottom.png");
        JLabel background1=new JLabel(image1);
        background1.setBounds(0,0,480,145);
        panel_bottom.add(background1);
        boxBottom=Box.createVerticalBox();
        boxBottom.add(panel_bottom);

        boxMain=Box.createVerticalBox();
        boxMain.add(boxUp);
        boxMain.add(Box.createVerticalStrut(25));
        boxMain.add(boxDown);
        add(boxMain,BorderLayout.CENTER);
        add(panel_bottom,BorderLayout.SOUTH);
    }

    /**
     * registerAction Listener
     * @author dzr
     * @version 1.0
     */
    private class registerAction implements ActionListener {
        @Override
        public void actionPerformed(ActionEvent e) {
            String name = name_text.getText();
            String pwd = new String(pwd_text.getPassword());
            String pwd_twice = new String(pwd_twice_text.getPassword());
            if(name.trim().isEmpty() || pwd_twice.trim().isEmpty() || pwd.trim().isEmpty()){
                new showMessageFrame("信息不能为空");
            }else{
                if(name.length() > 10){
                    new showMessageFrame("昵称不能超过10个字符");
                }else {
                    if(!pwd.equals(pwd_twice)){
                        new showMessageFrame("两次密码不一致");
                    }else {
                        String birthday = birthdayChooseBox.getComBox_year().getSelectedItem() + "-" + birthdayChooseBox.getComBox_month().getSelectedItem() + "-"+  birthdayChooseBox.getComBox_day().getSelectedItem();
                        String sex;
                        if (boy_button.isSelected()){
                            sex = boy_button.getActionCommand();
                        }else{
                            sex = girl_button.getActionCommand();
                        }
                        try {
                            int uid = Register.register(name, pwd, birthday, sex);
                            JOptionPane.showMessageDialog(
                                    RegisterFrame.this,
                                    "注册成功! " +"尊敬的" + name + ": 这是您的id号: " + uid + "\n 它是您的登录的唯一凭证，请牢记！",
                                    "温馨提示",
                                    JOptionPane.INFORMATION_MESSAGE
                            );
                            RegisterFrame.this.dispose();
                        } catch (IOException ioException) {
                            ioException.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
